package com.example.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.SysConst;
import com.example.web.dto.*;
import com.example.web.dto.query.*;
import com.example.web.entity.*;
import com.example.web.mapper.*;
import com.example.web.service.*;
import com.example.web.tools.dto.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;
import com.example.web.tools.*;
/**
 * 优惠卷领取记录功能实现类
 */
@Service
public class VolumeRecordServiceImpl extends ServiceImpl<VolumeRecordMapper, VolumeRecord> implements VolumeRecordService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的VolumeRecord表mapper对象
     */
    @Autowired
    private VolumeRecordMapper _VolumeRecordMpper;

 			
    @Autowired
    private VolumesMapper _VolumesMapper;                        
			
 			
    @Autowired
    private OrderInfoMapper _OrderInfoMapper;                        
			
 			
			

    /**
     * 优惠卷领取记录分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<VolumeRecordDto> List(VolumeRecordPagedInput input) {

      
        //声明一个支持优惠卷领取记录查询的(拉姆达)表达式
        LambdaQueryWrapper<VolumeRecord> queryWrapper = Wrappers.<VolumeRecord>lambdaQuery()
                .eq(input.getId()!=null&&input.getId()!=0,VolumeRecord::getId,input.getId())
                .eq(input.getCreatorId()!=null,VolumeRecord::getCreatorId,input.getCreatorId());
      
     	 //如果前端搜索传入VolumensId不为空,则进行精确查询
        if(input.getVolumensId()!=null) {
            queryWrapper=queryWrapper.eq(VolumeRecord::getVolumensId,input.getVolumensId());
       	 }
     	 //如果前端搜索传入UserId不为空,则进行精确查询
        if(input.getUserId()!=null) {
            queryWrapper=queryWrapper.eq(VolumeRecord::getUserId,input.getUserId());
       	 }
     	 //如果前端传入了UseTime时间范围,则进行时间范围搜索
        if(input.getUseTimeRange()!=null&&input.getUseTimeRange().stream().count()>1)
        {
            queryWrapper=queryWrapper.lt(VolumeRecord::getUseTime,input.getUseTimeRange().get(1));
            queryWrapper=queryWrapper.gt(VolumeRecord::getUseTime,input.getUseTimeRange().get(0));
        }
     	 //如果前端搜索传入OrderId不为空,则进行精确查询
        if(input.getOrderId()!=null) {
            queryWrapper=queryWrapper.eq(VolumeRecord::getOrderId,input.getOrderId());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(VolumeRecord::getCreationTime);
        //构建一个分页查询的model
        Page<VolumeRecord> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取优惠卷领取记录数据
        IPage<VolumeRecord> pageRecords= _VolumeRecordMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _VolumeRecordMpper.selectCount(queryWrapper);

        //把VolumeRecord实体转换成VolumeRecord传输模型
        List<VolumeRecordDto> items= Extension.copyBeanList(pageRecords.getRecords(),VolumeRecordDto.class);
        for (VolumeRecordDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto  CreatorAppUserDTO=new AppUserDto();
            AppUser  CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
           
          	            
           //查询出关联的Volumes表信息
           VolumesDto VolumensDTO = new VolumesDto();        
            Volumes  VolumensEntity= _VolumesMapper.selectOne(Wrappers.<Volumes>lambdaQuery().eq(Volumes::getId,item.getVolumensId()));
           if(VolumensEntity!=null) {  
          BeanUtils.copyProperties(VolumensDTO,VolumensEntity);
            item.setVolumensDto(VolumensDTO); 
           }
          
           
          	            
           //查询出关联的OrderInfo表信息
           OrderInfoDto OrderDTO = new OrderInfoDto();        
            OrderInfo  OrderEntity= _OrderInfoMapper.selectOne(Wrappers.<OrderInfo>lambdaQuery().eq(OrderInfo::getId,item.getOrderId()));
           if(OrderEntity!=null) {  
          BeanUtils.copyProperties(OrderDTO,OrderEntity);
            item.setOrderDto(OrderDTO); 
           }
          
           
          	            
           //查询出关联的AppUser表信息
           AppUserDto UserDTO = new AppUserDto();        
            AppUser  UserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getUserId()));
           if(UserEntity!=null) {  
          BeanUtils.copyProperties(UserDTO,UserEntity);
            item.setUserDto(UserDTO); 
           }
          
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个优惠卷领取记录查询
     */
    @SneakyThrows
    @Override
    public VolumeRecordDto Get(VolumeRecordPagedInput input) {
       if(input.getId()==null)
        {
            return new VolumeRecordDto();
        }
        PagedResult<VolumeRecordDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new VolumeRecordDto();
    }

    /**
     *优惠卷领取记录创建或者修改
     */
    @SneakyThrows
    @Override
    public VolumeRecordDto CreateOrEdit(VolumeRecordDto input) {

        //声明一个优惠卷领取记录实体
        VolumeRecord VolumeRecord = new VolumeRecord();

        //把前端传入的input参数拷贝到优惠卷领取记录实体
        BeanUtils.copyProperties(VolumeRecord,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(VolumeRecord);

        //定义一个返回给前端的优惠卷领取记录传输模型
        VolumeRecordDto VolumeRecordDto=new VolumeRecordDto();

        //同理把操作的优惠卷领取记录实体拷贝给优惠卷领取记录传输模型
        BeanUtils.copyProperties(VolumeRecordDto,VolumeRecord);

        //把传输模型返回给前端
        return VolumeRecordDto;
    }



}
